/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Speciality;
import net.gis.eemc.service.FacultyServiceImpl;
import net.gis.eemc.service.DepartmentServiceImpl;
import net.gis.eemc.service.SpecialityServiceImpl;
import net.gis.eemc.service.DisciplineServiceImpl;

/**
 *
 * @author Sergey
 */
@ManagedBean(name = "addDisciplineBean")
@ViewScoped
public class AddDisciplineBean {

    private Integer selectedFaculty = null;
    private Integer selectedDepartment = null;
    private List<SelectItem> selectFaculties = null;
    private Map<Integer, Faculty> facultyMap = null;
    private List<SelectItem> selectDepartments = null;
    private Map<Integer, Department> departmentMap = null;
    private FacultyServiceImpl facSrv = null;
    private DepartmentServiceImpl depSrv = null;
    private SpecialityServiceImpl specSrv = null;
    private DisciplineServiceImpl discipSrv = null;
    private Integer selectedSpecForUnselect = null;
    private Integer selectedSpecForSelect = null;
    private Map<Integer, Speciality> specMap = null;
    private List<SelectItem> selectedSpecialities = new LinkedList<SelectItem>();
    private List<SelectItem> availableSpecialities = null;
    private List<Speciality> specialities = null;
    private String code;
    private String disciplineName;
    private boolean lectures;
    private boolean practice;
    private boolean labs;
    private boolean quiz;
    private boolean exam;
    private boolean courseProject;
    private boolean test;

    private synchronized void fillSelectFaculties() {
        selectFaculties = new ArrayList<SelectItem>();
        facultyMap = new HashMap<Integer, Faculty>();
        selectFaculties.add(new SelectItem(0, new String()));
        for (Faculty fac : facSrv.getFaculties()) {
            facultyMap.put(fac.getIdfaculty(), fac);
            selectFaculties.add(new SelectItem(fac.getIdfaculty(), fac.getShortName()));
        }
    }

    private synchronized void fillSelectDepartments() {
        if (selectedFaculty != null) {
            selectDepartments = new ArrayList<SelectItem>();
            departmentMap = new HashMap<Integer, Department>();

            for (Department dep : depSrv.getDepartmentsForFaculty(selectedFaculty)) {
                departmentMap.put(dep.getIdDep(), dep);
                selectDepartments.add(new SelectItem(dep.getIdDep(), dep.getShortName()));
            }
        } else {
            selectedDepartment = null;
            departmentMap = null;
        }
    }

    private void initSpecLists() {
        List<Speciality> specLst = specSrv.getSpecialities();
        specMap = new HashMap<Integer, Speciality>(specLst.size());
        selectedSpecialities = new LinkedList<SelectItem>();
        availableSpecialities = new LinkedList<SelectItem>();
        specialities = new LinkedList<Speciality>();

        for (Speciality s : specLst) {
            specMap.put(s.getIdSpec(), s);
            availableSpecialities.add(new SelectItem(s.getIdSpec(), s.getTitle()));
        }
    }

    private void loadAvailableSpecList() {
        List<Speciality> specLst = specSrv.getSpecialities();
        specLst.removeAll(this.specialities);
        availableSpecialities.clear();

        for (Speciality s : specLst) {
            availableSpecialities.add(new SelectItem(s.getIdSpec(), s.getTitle()));
        }
    }

    private void loadSelectedSpecList() {
        selectedSpecialities.clear();
        for (Speciality spec : specialities) {
            selectedSpecialities.add(new SelectItem(spec.getIdSpec(), spec.getTitle()));
        }
    }

    /** Creates a new instance of AddDisciplineBean */
    public AddDisciplineBean() {
        facSrv = new FacultyServiceImpl();
        depSrv = new DepartmentServiceImpl();
        specSrv = new SpecialityServiceImpl();
        discipSrv = new DisciplineServiceImpl();

        fillSelectFaculties();
        fillSelectDepartments();

        initSpecLists();
    }

    public void Save() {
        try {
            discipSrv.saveDiscipline(departmentMap.get(selectedDepartment),
                    code,
                    disciplineName,
                    lectures,
                    practice,
                    labs,
                    quiz,
                    exam,
                    courseProject,
                    test,
                    specialities);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void addSpeciality() {
        try {
            if (selectedSpecForSelect != null) {
                Speciality spec = specMap.get(selectedSpecForSelect);
                selectedSpecialities.add(new SelectItem(spec.getIdSpec(), spec.getTitle()));
                specialities.add(spec);
                selectedSpecForSelect = null;
                selectedSpecForUnselect = null;
                loadAvailableSpecList();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void removeSpeciality() {
        try {
            if (selectedSpecForUnselect != null) {
                Speciality spec = specMap.get(selectedSpecForUnselect);
                specialities.remove(spec);
                loadSelectedSpecList();
                selectedSpecForSelect = null;
                selectedSpecForUnselect = null;
                loadAvailableSpecList();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Integer getSelectedFaculty() {
        return selectedFaculty;
    }

    public void setSelectedFaculty(Integer faculty) {
        this.selectedFaculty = faculty;
        fillSelectDepartments();
    }

    public Integer getSelectedDepartment() {
        return selectedDepartment;
    }

    public void setSelectedDepartment(Integer department) {
        this.selectedDepartment = department;
    }

    public List<SelectItem> getSelectFaculties() {
        return selectFaculties;
    }

    public List<SelectItem> getSelectDepartments() {
        return selectDepartments;
    }

    public Integer getSelectedSpecForSelect() {
        return selectedSpecForSelect;
    }

    public void setSelectedSpecForSelect(Integer selectedSpecForSelect) {
        this.selectedSpecForSelect = selectedSpecForSelect;
    }

    public Integer getSelectedSpecForUnselect() {
        return selectedSpecForUnselect;
    }

    public void setSelectedSpecForUnselect(Integer selectedSpecForUnselect) {
        this.selectedSpecForUnselect = selectedSpecForUnselect;
    }

    public List<SelectItem> getAvailableSpecialities() {
        return availableSpecialities;
    }

    public List<SelectItem> getSelectedSpecialities() {
        return selectedSpecialities;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isCourseProject() {
        return courseProject;
    }

    public void setCourseProject(boolean courseProject) {
        this.courseProject = courseProject;
    }

    public String getDisciplineName() {
        return disciplineName;
    }

    public void setDisciplineName(String disciplineName) {
        this.disciplineName = disciplineName;
    }

    public boolean isExam() {
        return exam;
    }

    public void setExam(boolean exam) {
        this.exam = exam;
    }

    public boolean isLabs() {
        return labs;
    }

    public void setLabs(boolean labs) {
        this.labs = labs;
    }

    public boolean isLectures() {
        return lectures;
    }

    public void setLectures(boolean lectures) {
        this.lectures = lectures;
    }

    public boolean isPractice() {
        return practice;
    }

    public void setPractice(boolean practice) {
        this.practice = practice;
    }

    public boolean isQuiz() {
        return quiz;
    }

    public void setQuiz(boolean quiz) {
        this.quiz = quiz;
    }

    public boolean isTest() {
        return test;
    }

    public void setTest(boolean test) {
        this.test = test;
    }
}
